package com.jytc.framework.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jytc.framework.constants.SessionNameConstants;

/**
 * 
 * @title: Session工具类
 * @description: 
 *
 * @author: dongyuese
 *
 */
public final class SessionUtils
{
	public static Object getObjectAttribute(HttpSession session, String objectName)
	{
		return session.getAttribute(objectName);
	}

	public static void setObjectAttribute(HttpSession session, String objectName, Object object)
	{
		session.setAttribute(objectName, object);
	}

	public static void removeObjectAttribute(HttpSession session, String objectName)
	{
		session.removeAttribute(objectName);
	}

	public static Object getObjectAttribute(HttpServletRequest request, String objectName)
	{
		return request.getSession().getAttribute(objectName);
	}

	public static void setObjectAttribute(HttpServletRequest request, String objectName, Object object)
	{
		request.getSession().setAttribute(objectName, object);
	}

	public static void removeObjectAttribute(HttpServletRequest request, String objectName)
	{
		request.getSession().removeAttribute(objectName);
	}

	/**
	 * 检查图形验证码是否正确
	 * 检查后自动移去Session中的验证码
	 * @param request
	 * @param checkCode
	 * @return
	 */
	public static boolean checkImgCheckCode(HttpServletRequest request, String checkCode)
	{
		boolean result = (checkCode != null
				&& SessionUtils.getObjectAttribute(request, SessionNameConstants.IMG_CHECK_CODE) != null && checkCode
				.compareTo(SessionUtils.getObjectAttribute(request, SessionNameConstants.IMG_CHECK_CODE).toString()) == 0);
		//		SessionUtils.removeObjectAttribute(request, SessionNameConstants.IMG_CHECK_CODE);
		return result;

	}

	/**
	 * 检查短信验证码是否正确
	 * 检查后自动移去Session中的验证码
	 * @param request
	 * @param checkCode
	 * @return
	 */
	public static boolean checkSMSCheckCode(HttpServletRequest request, String checkCode)
	{
		boolean result = (checkCode != null
				&& SessionUtils.getObjectAttribute(request, SessionNameConstants.SMS_CHECK_CODE) != null && checkCode
				.compareTo(SessionUtils.getObjectAttribute(request, SessionNameConstants.SMS_CHECK_CODE).toString()) == 0);
		SessionUtils.removeObjectAttribute(request, SessionNameConstants.SMS_CHECK_CODE);
		return result;

	}
}
